<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //原数组改变
        // splice()方法用于替换数组中的指定项
        var arr=['A','B','C','D','E','F','G'];
        var items1=arr.splice(3,2,'X','Y','Z');//从下标3开始，连续替换2项，替换为'X','Y','Z'
        console.log(arr);//['A','B','C','X','Y','Z','F','G']
        console.log(items1)//['D','E']

        //splice()方法 用于在指定位置插入新项
        var arr2=['A','B','C','D']
        var add2=arr2.splice(2,0,'x','y','z')//从下标2开始，连续替换0项，替换为'X','Y','Z',相当于插入
        console.log(arr2)//[A,B,x,y,z,C,D]
        console.log(add2)//[]

        //splice()方法 用于删除指定项
        var arr3 = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
        var del3=arr3.splice(3, 2)//从下标3开始，连续删除2项
        console.log(arr3)//['A', 'B', 'C', 'F', 'G']
        //splice()方法会以数组的形式返回被删除的项或被替换的项,添加的话就返回空数组
        console.log(del3)//['D', 'E']]

    </script>
</body>
</html>